package com.xunqi.mall.service.impl;

import com.xunqi.mall.consts.MallConst;
import com.xunqi.mall.mapper.CategoryMapper;
import com.xunqi.mall.pojo.Category;
import com.xunqi.mall.service.CategoryService;
import com.xunqi.mall.vo.CategoryVo;
import com.xunqi.mall.vo.ResponseVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Description:
 * @Created with IntelliJ IDEA.
 * @author: 夏沫止水
 * @create: 2020-04-23 19:40
 **/
@Service("categoryService")
public class CategoryServiceImpl implements CategoryService {

    @Resource
    private CategoryMapper categoryMapper;

    /**
     * 查询全部类目信息
     * @return
     */
    @Override
    public ResponseVo<List<CategoryVo>> selectAll() {

        //获取全部类目信息
        List<Category> categoryList = categoryMapper.selectAll();

        //把categoryList赋值转换成categoryVoList
        List<CategoryVo> categoryVoList = categoryList.stream()
                .filter(e -> e.getParentId().equals(MallConst.ROOT_PARENT_ID))
                .map(this::category2CategoryVo)
                //对一级目录进行排序倒序
                .sorted(Comparator.comparing(CategoryVo::getSortOrder).reversed())
                .collect(Collectors.toList());

        //查询子目录信息
        findSubCategory(categoryVoList,categoryList);

        return ResponseVo.success(categoryVoList);
    }

    @Override
    public void findSubCategoryId(Integer id, Set<Integer> resultSet) {

        //获取全部类目信息
        List<Category> categoryList = categoryMapper.selectAll();

        //查询子类目信息
        findSubCategoryId(id,resultSet,categoryList);
    }

    /**
     * 查询子类目信息
     * @param id
     * @param resultSet
     * @param categories
     */
    private void findSubCategoryId(Integer id, Set<Integer> resultSet,List<Category> categories) {
        for (Category category : categories) {
            if (category.getParentId().equals(id)) {
                resultSet.add(category.getId());

                //递归继续查询
                findSubCategoryId(category.getId(),resultSet,categories);
            }
        }
    }

    /**
     * 递归实现多级查询
     * @param categoryVoList
     * @param categories
     */
    private void findSubCategory(List<CategoryVo> categoryVoList,List<Category> categories) {
        for (CategoryVo categoryVo : categoryVoList) {
            //子目录list集合
            List<CategoryVo> subCategoryVoList = new ArrayList<>();

            for (Category category : categories) {
                //如果查到内容，设置子目录subCategory，继续往下查
                if (categoryVo.getId().equals(category.getParentId())) {
                    //进行对象之间的转换
                    CategoryVo subCategoryVo = category2CategoryVo(category);
                    //添加到子目录去
                    subCategoryVoList.add(subCategoryVo);
                }

                //进行排序,reversed()反转排序也就是倒序，默认是从小到大排序
                subCategoryVoList.sort(Comparator.comparing(CategoryVo::getSortOrder).reversed());

                //设置子目录，关联信息
                categoryVo.setSubCategories(subCategoryVoList);

                //在继续往下查询调用本方法，实现多级查询
                findSubCategory(subCategoryVoList,categories);
            }
        }
    }


    //进行转换
    private CategoryVo category2CategoryVo(Category category) {
        CategoryVo categoryVo = new CategoryVo();
        BeanUtils.copyProperties(category,categoryVo);
        return categoryVo;
    }
}
